package com.ptswitch.material.components;

import java.util.List;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.hxzon.tapestry5.util.RequestParameter;

import com.ptswitch.material.model.ContractItem;

public class ReceiveContractItems extends AbstractField {
	@Parameter(required = true, allowNull = false)
	private List<ContractItem> value;

	@Inject
	private Request request;

	@BeginRender
	void begin(MarkupWriter writer) {
		writer.element("table", "class", "list");
		writer.element("thead");
		writer.element("tr");
		writer.element("th");
		writer.write("物料编码");
		writer.end();
		writer.element("th");
		writer.write("物料名称");
		writer.end();
		writer.element("th");
		writer.write("规格型号");
		writer.end();
		writer.element("th");
		writer.write("PCB封装");
		writer.end();
		writer.element("th");
		writer.write("数量");
		writer.end();
		writer.element("th");
		writer.write("收货");
		writer.end();
		writer.end();// end tr
		writer.end();// end thead
		writer.element("tbody");
		int i = 0;
		for (ContractItem item : value) {
			writer.element("tr");
			writer.element("td");
			// writer.element("a", "class","forward","target","_blank","href",
			// request.getContextPath()+"/material/view/"+material.getId());
			writer.write(item.getCode());
			// writer.end();//end a
			writer.end();
			writer.element("td");
			writer.write(item.getName());
			writer.end();
			writer.element("td");
			writer.write(item.getSpecification());
			writer.end();
			writer.element("td");
			writer.write(item.getPcbDecal());
			writer.end();
			writer.element("td");
			writer.write(String.valueOf(item.getQuantities()));
			writer.end();
			writer.element("td");
			writer.writeRaw("已收" + item.getNumReceiving() + ",未收" + item.getNumUnReceiving() + "<br />");
			writer.writeRaw("本次收货");
			writer.element("input", "type", "text", "name", "item" + i + ".numReceiving", "value", item.getNumCurrentReceive());
			writer.end();
			writer.end();//end td
			writer.end();// end tr
			i++;
		}
		writer.end();// end tbody
		writer.end();// end table
	}

	@Override
	protected void processSubmission(String elementName) {
		int itemSize = value.size();
		ContractItem contractItem;
		for (int i = itemSize - 1; i >= 0; i--) {
			contractItem = value.get(i);
			int numReceiving = RequestParameter.getIntParameter(request, "item" + i + ".numReceiving");
			contractItem.setNumCurrentReceive(numReceiving);
//			if (numReceiving > contractItem.getQuantities()) {
////				tracker.recordInput(field, input);
//				throw AppRuntimeException.error(contractItem.getCode() + "收货数大于购买数量");
//			}
		}
	}
}
